package com.study.talk.data.repository

import android.util.Log
import com.study.talk.data.model.MomentComment
import com.study.talk.data.model.MomentLike
import com.study.talk.data.model.User
import com.study.talk.data.model.resp.MomentResponse
import com.study.talk.data.model.vo.MomentCommentVo
import com.study.talk.data.model.vo.MomentLikeVo
import com.study.talk.data.model.vo.MomentVo
import com.study.talk.data.remote.MomentApi
import javax.inject.Inject

class MomentRepository @Inject constructor(private val momentApi: MomentApi) : BaseRepository() {

    companion object {
        private const val TAG = "MomentRepository"
    }

    suspend fun addMoment(momentVo: MomentVo): Boolean {
        val response = momentApi.addMoment(momentVo)
        if (response.isSuccessful) {
            if (response.body()?.status == 200) {
                if (response.body()?.data == null) {
                    Log.e(TAG, "friendNotInGroup: response.body()?.data is null")
                    return false
                } else {
                    return response.body()?.data!!
                }
            } else {
                Log.e(TAG, "friendNotInGroup: error,msg:${response.body()?.message}")
                return false
            }
        } else {
            Log.e(TAG, "friendNotInGroup: request fail,msg:${response.message()}")
            return false
        }
    }

    suspend fun getMoments(
        userId: Int, success: (List<MomentResponse>) -> Unit, fail: (String) -> Unit
    ) {
        val response = momentApi.getUserMoments(userId)
        handleResponse("getMoments", response, success, fail)
    }

    //TODO 获取自己的朋友圈就不需要传userId
    suspend fun getUserMoments(
        userId: Int, success: (List<MomentResponse>) -> Unit, fail: (String) -> Unit
    ) {
        val response = momentApi.getMyMoments(userId)
        handleResponse("getUserMoments", response, success, fail)
    }

    suspend fun updateMomentLike(
        momentLikeVo: MomentLikeVo, success: (Int) -> Unit = {}, fail: (String) -> Unit = {}
    ): Int {
        val response = momentApi.updateMomentLike(momentLikeVo)
        return handleResponse("updateMomentLike", response, success, fail) ?: 0
    }

    suspend fun getLikeList(
        momentId: Int, success: (List<MomentLike>) -> Unit = {}, fail: (String) -> Unit = {}
    ): List<MomentLike>? {
        val response = momentApi.getLikes(momentId)
        return handleResponse("getLikeList", response, success, fail)
    }

    suspend fun addComment(
        momentCommentVo: MomentCommentVo,
        success: (List<MomentComment>) -> Unit = {},
        fail: (String) -> Unit = {}
    ): List<MomentComment>? {
        val response = momentApi.addComment(momentCommentVo)
        return handleResponse("addComment", response, success, fail)
    }

    suspend fun deleteComment(
        momentComment: MomentComment, success: (List<MomentComment>) -> Unit = {},
        fail: (String) -> Unit = {}
    ): List<MomentComment>? {
        val response = momentApi.deleteComment(momentComment)
        return handleResponse("deleteComment",response,success,fail)
    }

}